/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.DeliverBillBean;
import com.minierp.bean.DeliverBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.DeliverBillDao;
import com.minierp.dao.DeliverBillItemDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.BusinessBillItemExt;
import com.minierp.model.CodeRule;
import com.minierp.model.DeliverBill;
import com.minierp.model.DeliverBillItem;
import com.minierp.model.SystemLog;
import com.minierp.service.DeliverBillService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeliverBillServiceImpl implements DeliverBillService {
	public void txInsert(DeliverBillBean form, String autoComp, UserBean ub,
			DeliverBillDao deliverBillDao,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillItemDao businessBillItemDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		DeliverBill obj = form.getDeliverBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(DeliverBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
					.getCodeRule(), "", form.getDeliverBill().getTallySetId()));

			for (DeliverBill deliList = deliverBillDao.get(obj.getBillCode()); deliList != null; deliList = deliverBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getDeliverBill()
						.getTallySetId()));
			}
		}

		List arg12 = deliverBillDao.getListByBusinessBill(obj
				.getBusinessBillId());

		for (int j = 0; j < arg12.size(); ++j) {
			DeliverBill rb = (DeliverBill) arg12.get(j);
			if (rb.getExamined().equals("N")) {
				throw new NotAllowException(
						"该业务单存在未审核的送货单，送货单号为"
								+ rb.getBillCode()
								+ "，请进入【客户业务-送货单一览表】找到这张送货单，把这张之前开过的未审核送货单审核通过就可以了，不需要再开送货单。");
			}
		}

		if (obj.getExamined().equals("Y")) {
			obj.setExamineTime(new Date());
		}

		deliverBillDao.save(obj);
		this.saveItems(form, autoComp, ub, deliverBillItemDao,
				businessBillItemDao, idGenerator);
	}

	public Map txInsertBatch(String[] idArr, String autoComp, UserBean ub,
			CodeRule dbCr, DeliverBillDao deliverBillDao,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillItemDao businessBillItemDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		if (dbCr == null) {
			throw new Exception("Code rule not found for deliver bill");
		} else {
			String dbIdStr = "";
			HashMap map = new HashMap();

			for (int i = 0; i < idArr.length; ++i) {
				long businessBillId = Long.parseLong(idArr[i]);
				BusinessBill b = (BusinessBill) businessBillItemDao.get(
						BusinessBill.class, businessBillId);
				BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
						BusinessBillExt.class, businessBillId);
				List bbItemList = businessBillItemDao
						.getListWidthExt(businessBillId);
				List deliList = deliverBillDao
						.getListByBusinessBill(businessBillId);

				DeliverBill deliverBill;
				for (int form = 0; form < deliList.size(); ++form) {
					deliverBill = (DeliverBill) deliList.get(form);
					if (deliverBill.getExamined().equals("N")) {
						throw new NotAllowException(
								"业务单"
										+ b.getBillCode()
										+ "存在未审核的送货单，送货单号为"
										+ deliverBill.getBillCode()
										+ "，请进入【客户业务-送货单一览表】找到这张送货单，把这张之前开过的未审核送货单审核通过就可以了，不需要再开送货单。");
					}
				}

				DeliverBillBean arg28 = new DeliverBillBean();
				deliverBill = new DeliverBill();
				deliverBill.setBusinessBillId(businessBillId);
				deliverBill.setCreaterId(ub.getStaffer().getId());
				deliverBill.setCreaterName(ub.getStaffer().getStafferName());
				deliverBill.setExaminerId(ub.getStaffer().getId());
				deliverBill.setExaminerName(ub.getStaffer().getStafferName());
				if (b != null) {
					deliverBill.setAddress(b.getAddress());
					deliverBill.setPhone(b.getPhone());
					deliverBill.setLinkman(b.getLinkman());
					deliverBill.setContent(b.getContent());
					deliverBill.setMark(b.getLadingRequire());
					if (deliverBill.getMark() != null
							&& deliverBill.getMark().length() > 100) {
						deliverBill.setMark(deliverBill.getMark().substring(0,
								90)
								+ "...");
					}
				}

				deliverBill.setTallySetId(ub.getUser().getDfTsId());
				deliverBill.setCreateTime(new Date());
				deliverBill
						.setId(idGenerator.getId(DeliverBill.class.getName()));
				if (dbCr.getGenerateRule().equals("MANUAL")) {
					throw new NotAllowException("送货单编码规则必须设置为自动产生！");
				}

				deliverBill
						.setBillCode(PageHelper.generateCode(serialNumberDao,
								dbCr, "", deliverBill.getTallySetId()));

				for (DeliverBill itemList = deliverBillDao.get(deliverBill
						.getBillCode()); itemList != null; itemList = deliverBillDao
						.get(deliverBill.getBillCode())) {
					deliverBill.setBillCode(PageHelper.generateCode(
							serialNumberDao, dbCr, "",
							deliverBill.getTallySetId()));
				}

				deliverBill.setExamined("Y");
				deliverBill.setExamineTime(new Date());
				deliverBillDao.save(deliverBill);
				ArrayList arg27 = new ArrayList();

				for (int j = 0; j < bbItemList.size(); ++j) {
					Object[] objs = (Object[]) bbItemList.get(j);
					BusinessBillItem bbItem = (BusinessBillItem) objs[0];
					BusinessBillItemExt bbIe = (BusinessBillItemExt) objs[1];
					DeliverBillItemBean cb = new DeliverBillItemBean();
					DeliverBillItem c = new DeliverBillItem();
					c.setBbItemId(bbItem.getId());
					c.setAmount(bbItem.getAmount().subtract(bbIe.getDeliAmou()));
					cb.setDeliverBillItem(c);
					arg27.add(cb);
				}

				arg28.setItems(arg27);
				arg28.setDeliverBill(deliverBill);
				this.saveItems(arg28, b, be, autoComp, ub, bbItemList,
						deliverBillItemDao, idGenerator);
				if (!dbIdStr.equals("")) {
					dbIdStr = dbIdStr + ";";
				}

				dbIdStr = dbIdStr + deliverBill.getId();
			}

			map.put("idStr", dbIdStr);
			return map;
		}
	}

	public String txInsertApp(String bbCode, String autoComp, UserBean ub,
			CodeRule dbCr, DeliverBillDao deliverBillDao,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		if (dbCr == null) {
			throw new NotAllowException("Code rule not found for deliver bill");
		} else {
			BusinessBill b = businessBillDao.get(bbCode);
			if (b == null) {
				throw new NotAllowException("找不到单号为" + bbCode + "的业务单");
			} else {
				BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
						BusinessBillExt.class, b.getId());
				if (be.getExamined().equals("N")) {
					throw new NotAllowException("业务单未审核，不能开送货单");
				} else if (be.getClosed().equals("Y")) {
					throw new NotAllowException("业务单已结清，不能开送货单");
				} else if (b.getTallySetId() != ub.getUser().getDfTsId()) {
					throw new NotAllowException("业务单帐套与当前开单帐套不一致，不能开送货单");
				} else {
					List bbItemList = businessBillItemDao.getListWidthExt(b
							.getId());
					ArrayList itemList = new ArrayList();

					for (int deliList = 0; deliList < bbItemList.size(); ++deliList) {
						Object[] form = (Object[]) bbItemList.get(deliList);
						BusinessBillItem deliverBill = (BusinessBillItem) form[0];
						BusinessBillItemExt d = (BusinessBillItemExt) form[1];
						if (deliverBill.getAmount().compareTo(d.getDeliAmou()) > 0) {
							DeliverBillItemBean cb = new DeliverBillItemBean();
							DeliverBillItem c = new DeliverBillItem();
							c.setBbItemId(deliverBill.getId());
							c.setAmount(deliverBill.getAmount().subtract(
									d.getDeliAmou()));
							cb.setDeliverBillItem(c);
							itemList.add(cb);
						}
					}

					if (itemList.size() == 0) {
						throw new NotAllowException("业务单已经送完货，不需要再开送货单。");
					} else {
						List arg20 = deliverBillDao.getListByBusinessBill(b
								.getId());

						DeliverBill arg23;
						for (int arg21 = 0; arg21 < arg20.size(); ++arg21) {
							arg23 = (DeliverBill) arg20.get(arg21);
							if (arg23.getExamined().equals("N")) {
								throw new NotAllowException("业务单存在未审核的送货单"
										+ arg23.getBillCode() + "，不能再开送货单");
							}
						}

						DeliverBillBean arg22 = new DeliverBillBean();
						arg23 = new DeliverBill();
						arg23.setBusinessBillId(b.getId());
						arg23.setCreaterId(ub.getStaffer().getId());
						arg23.setCreaterName(ub.getStaffer().getStafferName());
						arg23.setExaminerId(ub.getStaffer().getId());
						arg23.setExaminerName(ub.getStaffer().getStafferName());
						arg23.setAddress(b.getAddress());
						arg23.setPhone(b.getPhone());
						arg23.setLinkman(b.getLinkman());
						arg23.setContent(b.getContent());
						arg23.setMark(b.getLadingRequire());
						if (arg23.getMark() != null
								&& arg23.getMark().length() > 100) {
							arg23.setMark(arg23.getMark().substring(0, 90)
									+ "...");
						}

						arg23.setTallySetId(ub.getUser().getDfTsId());
						arg23.setCreateTime(new Date());
						arg23.setId(idGenerator.getId(DeliverBill.class
								.getName()));
						if (dbCr.getGenerateRule().equals("MANUAL")) {
							throw new NotAllowException("送货单编码规则必须设置为自动产生！");
						} else {
							arg23.setBillCode(PageHelper.generateCode(
									serialNumberDao, dbCr, "",
									arg23.getTallySetId()));

							for (DeliverBill arg24 = deliverBillDao.get(arg23
									.getBillCode()); arg24 != null; arg24 = deliverBillDao
									.get(arg23.getBillCode())) {
								arg23.setBillCode(PageHelper.generateCode(
										serialNumberDao, dbCr, "",
										arg23.getTallySetId()));
							}

							arg23.setExamined("Y");
							arg23.setExamineTime(new Date());
							deliverBillDao.save(arg23);
							arg22.setItems(itemList);
							arg22.setDeliverBill(arg23);
							this.saveItems(arg22, b, be, autoComp, ub,
									bbItemList, deliverBillItemDao, idGenerator);
							return arg23.getBillCode();
						}
					}
				}
			}
		}
	}

	public void txUpdate(DeliverBillBean form, String autoComp, UserBean ub,
			DeliverBillDao deliverBillDao,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator)
			throws Exception {
		DeliverBill obj = form.getDeliverBill();
		Object[] sv = deliverBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for deliver bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("送货单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException("送货单已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException("送货单已审核，不能修改！");
			} else {
				if (obj.getExamined().equals("Y")) {
					obj.setExamineTime(new Date());
				}

				obj.setUpdateTime(new Date());
				deliverBillDao.update(obj);
				this.saveItems(form, autoComp, ub, deliverBillItemDao,
						businessBillItemDao, idGenerator);
			}
		}
	}

	private void saveItems(DeliverBillBean form, BusinessBill b,
			BusinessBillExt be, String autoComp, UserBean ub, List bbItemList,
			DeliverBillItemDao deliverBillItemDao, IdGenerator idGenerator)
			throws NotAllowException {
		List items = form.getItems();
		String errorMsg = "";
		if (b == null) {
			errorMsg = "找不到id为" + form.getDeliverBill().getBusinessBillId()
					+ "的业务单";
		} else if (be.getExamined().equals("N")) {
			errorMsg = "业务单还没审核，不能送货。单号：" + b.getBillCode();
		} else if (be.getEnabled().equals("N")) {
			errorMsg = "业务单已作废，不能送货。单号：" + b.getBillCode();
		} else if (be.getClosed().equals("Y")) {
			errorMsg = "业务单已结清，不能送货。单号：" + b.getBillCode();
		}

		if (!errorMsg.equals("")) {
			throw new NotAllowException(errorMsg);
		} else {
			int newNum = 0;

			for (int curItemId = 0; curItemId < items.size(); ++curItemId) {
				DeliverBillItemBean bean = (DeliverBillItemBean) items
						.get(curItemId);
				if (bean.getOldId() == 0L
						&& bean.getDeliverBillItem().getAmount()
								.compareTo(new BigDecimal(0)) != 0) {
					++newNum;
				}
			}

			long arg23 = 0L;
			if (newNum > 0) {
				arg23 = idGenerator.getId(DeliverBillItem.class.getName(),
						newNum);
			}

			for (int delivered = 0; delivered < items.size(); ++delivered) {
				DeliverBillItemBean hasItemDeli = (DeliverBillItemBean) items
						.get(delivered);
				long j = hasItemDeli.getDeliverBillItem().getBbItemId();
				hasItemDeli.getDeliverBillItem().setBillId(
						form.getDeliverBill().getId());
				boolean bbItem = false;
				if (hasItemDeli.getOldId() != 0L) {
					if (hasItemDeli.getDeliverBillItem().getAmount()
							.compareTo(new BigDecimal(0)) == 0) {
						deliverBillItemDao.delete(hasItemDeli
								.getDeliverBillItem().getId());
					} else {
						deliverBillItemDao.update(hasItemDeli
								.getDeliverBillItem());
						bbItem = true;
					}
				} else if (hasItemDeli.getDeliverBillItem().getAmount()
						.compareTo(new BigDecimal(0)) != 0) {
					hasItemDeli.getDeliverBillItem().setId(arg23);
					deliverBillItemDao.save(hasItemDeli.getDeliverBillItem());
					++arg23;
					bbItem = true;
				}

				BusinessBillItem bbIe = null;
				BusinessBillItemExt bbIe1 = null;
				boolean foundBbItem = false;

				for (int deliveredAmount = 0; deliveredAmount < bbItemList
						.size(); ++deliveredAmount) {
					Object[] objs1 = (Object[]) bbItemList.get(deliveredAmount);
					bbIe = (BusinessBillItem) objs1[0];
					bbIe1 = (BusinessBillItemExt) objs1[1];
					if (bbIe.getId() == j) {
						foundBbItem = true;
						break;
					}
				}

				if (foundBbItem) {
					if (bbItem) {
						BigDecimal arg34 = deliverBillItemDao
								.getDeliveredAmount(j);
						if (arg34.compareTo(bbIe.getAmount()) == 1) {
							throw new NotAllowException("送货数量不能超过订货数量，请修正！");
						}
					}

					if (form.getDeliverBill().getExamined().equals("Y")
							&& bbItem) {
						bbIe1.setDeliAmou(bbIe1.getDeliAmou().add(
								hasItemDeli.getDeliverBillItem().getAmount()));
						deliverBillItemDao.update(bbIe1);
					}
				}
			}

			if (autoComp.equals("Y")) {
				boolean arg24 = true;

				for (int arg25 = 0; arg25 < bbItemList.size(); ++arg25) {
					Object[] arg28 = (Object[]) bbItemList.get(arg25);
					BusinessBillItem objs = (BusinessBillItem) arg28[0];
					BusinessBillItemExt arg31 = (BusinessBillItemExt) arg28[1];
					if (objs.getAmount().compareTo(arg31.getDeliAmou()) == 0) {
						if (objs.getIsCoop().equals("Y")
								&& arg31.getCoopComp().equals("N")) {
							arg31.setCoopComp("Y");
							arg31.setOperTime(new Date());
							arg31.setOperId(ub.getUser().getStafferId());
							arg31.setOperName(ub.getStaffer().getStafferName());
						}

						if (objs.getWcId() != 0L
								&& arg31.getWcComp().equals("N")) {
							arg31.setWcComp("Y");
							arg31.setOperTime(new Date());
							arg31.setOperId(ub.getUser().getStafferId());
							arg31.setOperName(ub.getStaffer().getStafferName());
						}

						deliverBillItemDao.update(arg31);
					} else {
						arg24 = false;
					}
				}

				if (arg24) {
					be.setCompleted("Y");
					be.setCompleteTime(new Date());
				}
			}

			String arg26 = "Y";
			String arg27 = "N";

			for (int arg29 = 0; arg29 < bbItemList.size(); ++arg29) {
				Object[] arg30 = (Object[]) bbItemList.get(arg29);
				BusinessBillItem arg32 = (BusinessBillItem) arg30[0];
				BusinessBillItemExt arg33 = (BusinessBillItemExt) arg30[1];
				if (arg33.getDeliAmou().compareTo(new BigDecimal(0)) == 0) {
					arg26 = "N";
					if (arg27.equals("Y")) {
						arg26 = "P";
						break;
					}
				} else {
					if (arg32.getAmount().compareTo(arg33.getDeliAmou()) != 0) {
						arg26 = "P";
						break;
					}

					arg27 = "Y";
				}
			}

			be.setDelivered(arg26);
			be.setHasDeliver("Y");
			deliverBillItemDao.update(be);
		}
	}

	private void saveItems(DeliverBillBean form, String autoComp, UserBean ub,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator)
			throws NotAllowException {
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, form.getDeliverBill().getBusinessBillId());
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, form.getDeliverBill()
						.getBusinessBillId());
		List bbItemList = businessBillItemDao.getListWidthExt(b.getId());
		this.saveItems(form, b, be, autoComp, ub, bbItemList,
				deliverBillItemDao, idGenerator);
	}

	public void txExamine(long id, String autoComp, UserBean ub,
			DeliverBillDao deliverBillDao,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillItemDao businessBillItemDao) throws Exception {
		DeliverBill a = (DeliverBill) deliverBillDao.get(DeliverBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			List itemList = deliverBillItemDao.getListByDeliverBill(a.getId());
			a.setExamined("Y");
			a.setExamineTime(new Date());
			a.setExaminerId(ub.getStaffer().getId());
			a.setExaminerName(ub.getStaffer().getStafferName());
			a.setUpdateTime(new Date());
			deliverBillDao.update(a);
			this.updateBusinessBill(itemList, autoComp, ub,
					a.getBusinessBillId(), true, false, businessBillItemDao,
					deliverBillDao);
		}

	}

	private void updateBusinessBill(List<?> itemList, String autoComp,
			UserBean ub, long bbId, boolean isAdd, boolean checkHasDeliver,
			BusinessBillItemDao businessBillItemDao,
			DeliverBillDao deliverBillDao) throws NotAllowException {
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, bbId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, bbId);
		String errorMsg = "";
		if (b == null) {
			errorMsg = "找不到id为" + bbId + "的业务单";
		} else if (be.getExamined().equals("N")) {
			errorMsg = "单号为" + b.getBillCode() + "的业务单还没审核，不能操作";
		} else if (be.getEnabled().equals("N")) {
			errorMsg = "单号为" + b.getBillCode() + "的业务单已作废，不能操作";
		} else if (be.getClosed().equals("Y")) {
			errorMsg = "单号为" + b.getBillCode() + "的业务单已结清，不能操作";
		}

		if (!errorMsg.equals("")) {
			throw new NotAllowException(errorMsg);
		} else {
			List bbItemList = businessBillItemDao.getListWidthExt(b.getId());

			for (int delivered = 0; delivered < itemList.size(); ++delivered) {
				DeliverBillItem hasItemDeli = (DeliverBillItem) itemList
						.get(delivered);
				BigDecimal j;
				if (isAdd) {
					j = hasItemDeli.getAmount();
				} else {
					j = (new BigDecimal(0)).subtract(hasItemDeli.getAmount());
				}

				for (int objs = 0; objs < bbItemList.size(); ++objs) {
					Object[] bbItem = (Object[]) bbItemList.get(objs);
					BusinessBillItem bbIe = (BusinessBillItem) bbItem[0];
					BusinessBillItemExt bbIe1 = (BusinessBillItemExt) bbItem[1];
					if (bbIe.getId() == hasItemDeli.getBbItemId()) {
						bbIe1.setDeliAmou(bbIe1.getDeliAmou().add(j));
						businessBillItemDao.update(bbIe1);
						break;
					}
				}
			}

			if (autoComp.equals("Y")) {
				boolean arg20 = true;

				for (int arg21 = 0; arg21 < bbItemList.size(); ++arg21) {
					Object[] arg25 = (Object[]) bbItemList.get(arg21);
					BusinessBillItem arg27 = (BusinessBillItem) arg25[0];
					BusinessBillItemExt arg29 = (BusinessBillItemExt) arg25[1];
					if (arg27.getAmount().compareTo(arg29.getDeliAmou()) == 0) {
						if (arg27.getIsCoop().equals("Y")
								&& arg29.getCoopComp().equals("N")) {
							arg29.setCoopComp("Y");
							arg29.setOperTime(new Date());
							arg29.setOperId(ub.getUser().getStafferId());
							arg29.setOperName(ub.getStaffer().getStafferName());
						}

						if (arg27.getWcId() != 0L
								&& arg29.getWcComp().equals("N")) {
							arg29.setWcComp("Y");
							arg29.setOperTime(new Date());
							arg29.setOperId(ub.getUser().getStafferId());
							arg29.setOperName(ub.getStaffer().getStafferName());
						}

						businessBillItemDao.update(arg29);
					} else {
						arg20 = false;
					}
				}

				if (arg20) {
					be.setCompleted("Y");
					be.setCompleteTime(new Date());
				}
			}

			if (checkHasDeliver) {
				List arg22 = deliverBillDao.getListByBusinessBill(b.getId());
				if (arg22.size() > 0) {
					be.setHasDeliver("Y");
				} else {
					be.setHasDeliver("N");
				}
			}

			String arg23 = "Y";
			String arg24 = "N";

			for (int arg26 = 0; arg26 < bbItemList.size(); ++arg26) {
				Object[] arg28 = (Object[]) bbItemList.get(arg26);
				BusinessBillItem arg30 = (BusinessBillItem) arg28[0];
				BusinessBillItemExt arg31 = (BusinessBillItemExt) arg28[1];
				if (arg31.getDeliAmou().compareTo(new BigDecimal(0)) == 0) {
					arg23 = "N";
					if (arg24.equals("Y")) {
						arg23 = "P";
						break;
					}
				} else {
					if (arg30.getAmount().compareTo(arg31.getDeliAmou()) != 0) {
						arg23 = "P";
						break;
					}

					arg24 = "Y";
				}
			}

			be.setDelivered(arg23);
			businessBillItemDao.update(be);
		}
	}

	public void txUnExamine(long id, String autoComp, UserBean ub,
			DeliverBillDao deliverBillDao,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillItemDao businessBillItemDao) throws Exception {
		DeliverBill a = (DeliverBill) deliverBillDao.get(DeliverBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			deliverBillDao.update(a);
			List itemList = deliverBillItemDao.getListByDeliverBill(a.getId());
			this.updateBusinessBill(itemList, autoComp, ub,
					a.getBusinessBillId(), false, false, businessBillItemDao,
					deliverBillDao);
		}

	}

	public void txCancel(long id, String reason, String autoComp, UserBean ub,
			DeliverBillDao deliverBillDao,
			DeliverBillItemDao deliverBillItemDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator)
			throws Exception {
		DeliverBill d = (DeliverBill) deliverBillDao.get(DeliverBill.class, id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			deliverBillDao.update(d);
			if (d.getExamined().equals("Y")) {
				List log = deliverBillItemDao.getListByDeliverBill(d.getId());
				this.updateBusinessBill(log, autoComp, ub,
						d.getBusinessBillId(), false, true,
						businessBillItemDao, deliverBillDao);
			} else {
				BusinessBillExt log1 = (BusinessBillExt) deliverBillDao.get(
						BusinessBillExt.class, d.getBusinessBillId());
				String errorMsg = "";
				if (log1 == null) {
					errorMsg = "找不到id为" + d.getBusinessBillId() + "的业务单";
				}

				if (!errorMsg.equals("")) {
					throw new NotAllowException(errorMsg);
				}

				List iList = deliverBillDao.getListByBusinessBill(d
						.getBusinessBillId());
				if (iList.size() > 0) {
					log1.setHasDeliver("Y");
				} else {
					log1.setHasDeliver("N");
				}

				deliverBillDao.update(log1);
			}

			SystemLog log2 = new SystemLog();
			log2.setId(idGenerator.getId(SystemLog.class.getName()));
			log2.setActionCode("DELIVER_BILL");
			log2.setLogType("CANCEL_LOG");
			log2.setCreaterId(ub.getStaffer().getId());
			log2.setCreaterName(ub.getStaffer().getStafferName());
			log2.setDepName(ub.getDepName());
			log2.setDepId(ub.getStaffer().getDepartmentId());
			log2.setStatus("S");
			log2.setUserName(ub.getUser().getUserName());
			log2.setIp(d.getBillCode());
			log2.setMark("");
			log2.setRelId(id);
			log2.setCancelReason(reason);
			log2.setTallySetId(d.getTallySetId());
			deliverBillDao.save(log2);
		}

	}
}